package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.PlayRecord;
import com.softeem.music_sys.service.PlayRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "播放记录接口", description = "播放记录相关操作：增删改查等")
@RestController
@RequestMapping("/playRecord")
public class PlayRecordController {

    @Autowired
    private PlayRecordService playRecordService;

    // 添加播放记录
    @Operation(summary = "添加播放记录", description = "添加播放记录，提供JSON格式播放记录数据")
    @PostMapping
    public Result add(@RequestBody PlayRecord playRecord) {
        boolean flag = playRecordService.save(playRecord);
        return flag ? Result.success().setMsg("添加成功").setData(playRecord) : Result.fail("添加失败");
    }

    // 删除播放记录
    @Operation(summary = "删除播放记录", description = "根据播放记录ID删除播放记录")
    @Parameters({
            @Parameter(name = "id", description = "播放记录ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查播放记录是否存在
        PlayRecord playRecord = playRecordService.getById(id);
        if (playRecord == null) {
            return Result.fail("播放记录不存在");
        }
        boolean flag = playRecordService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 播放记录列表查询（分页）
    @Operation(summary = "播放记录列表查询", description = "播放记录列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<PlayRecord> pageUtil = new Page<>(page, limit);
        List<PlayRecord> list = playRecordService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取播放记录信息
    @Operation(summary = "获取播放记录信息", description = "根据播放记录ID获取播放记录详细信息")
    @Parameters({
            @Parameter(name = "id", description = "播放记录ID")
    })
    @GetMapping("/{id}")
    public Result getPlayRecordById(@PathVariable Integer id) {
        PlayRecord playRecord = playRecordService.getById(id);
        if (playRecord == null) {
            return Result.fail("播放记录不存在");
        }
        return Result.success().setData(playRecord);
    }

    // 根据用户ID查询播放记录列表
    @Operation(summary = "根据用户ID查询播放记录", description = "根据用户ID查询该用户的所有播放记录")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID")
    })
    @GetMapping("/byUser/{userId}")
    public Result getPlayRecordByUserId(@PathVariable Integer userId) {
        List<PlayRecord> list = playRecordService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<PlayRecord>().eq("user_id", userId));
        return Result.success().setData(list);
    }

    // 根据音乐ID查询播放记录列表
    @Operation(summary = "根据音乐ID查询播放记录", description = "根据音乐ID查询该音乐的所有播放记录")
    @Parameters({
            @Parameter(name = "musicId", description = "音乐ID")
    })
    @GetMapping("/byMusic/{musicId}")
    public Result getPlayRecordByMusicId(@PathVariable Integer musicId) {
        List<PlayRecord> list = playRecordService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<PlayRecord>().eq("music_id", musicId));
        return Result.success().setData(list);
    }
}